package com.ship.auth;

import com.alibaba.fastjson.JSONObject;
import com.banboocloud.Codec.Sm3Signature;
import com.banboocloud.Codec.Sm4Cipher;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class test {
//    public static void main(String[] args) {
//        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//        String passwd =  passwordEncoder.encode("Lng@yscb2023");
//        System.out.println(passwd);
//    }


    //解密
    public static void main(String[] args) {
//        String key = "X2!k4@L7#q9M6p8R";//测试环境
//        String key = "entrade-gaspower";//测试环境
//        String key = "F9#q7A@3r5G6p2WR";//生产环境
        String key = "F9#q7A@3r5G6p2WR";//生产环境
        Sm4Cipher sm4Cipher = new Sm4Cipher();
//        String ciphertext = "nJ/refvXOfE/tEhRB/AiFZzj6SiNy9d7vaOJVleeoDS8zMXta1QdF+DJ9qGLW0TeIL3+6DepwWSmfPK26jCbZ+JUQ1KWRJqR5X53a0vQP4v3hbHVeHqoIXTFitRxOaeG5NXPDHqFb2abS3uHdAgM+ZBikL8HdWLbjyXrMOz/feXEYnKYQ+zP4F6J5JAdd0fc6L2IGgpwxyeAuYiisHTyPw==";
//        String ciphertext = "8mFYSBDz4CSa9cDgQxfsivwyQdnuPs3os2k0Ux3DtlD8jh+4JNmLaYZYRaGSlMfLKE8Xd957N5cc8EMLptJYjEoSAos81lbzO8chssHRNOVVeraQ11VOmhmVo9ru3SLZBR+3ouUoEgBEHy3v9tGzWeKx5qahpPhfZ+giYFPHz112/hvetPyd85016XyyzRXy\n";
//        String ciphertext = "xH30adB2X6NmF+GGbbxmOa+y14RHtfCsHEfS5Gt+lQ4=";


        String ciphertext = "OzM4XZj5s99aYeXCBLwi/HjDN9lh9ehXRW9wtyzmwb1pLnt6to5JtZVqnHahw4OjUkoZJrA6DqYI8yBpdS7annSXOqADFUUppAcZFK8vTWHGVlbxjcXYp2dj0dlG/61f7pKr/oYKPq1j8u1uFrqxglU63g2SpHCPOsMeM8xm12b2gWxJhFTKXrZ2w9kuuMew9JxoUrYo9HyJi8HF7XNhphFc1oBvblf36PqDlzZ7CsgiT1k35b6CKHyh5Ipbd0R1tli7yCh2v+AGS0D/Yig5TeMyFaafD8qbOwjMF2GYgF4=";
        String result2 = sm4Cipher.decrypt(ciphertext, key);
        System.out.println(result2);
    }

//    public static void main(String[] args) {
//        String key = "X2!k4@L7#q9M6p8R";//测试环境
////        String key = "F9#q7A@3r5G6p2WR";//生产环境
//        Sm4Cipher sm4Cipher = new Sm4Cipher();
//        Map<String, Object> result = new HashMap<>();
//        result.put("bimRequestId", "测试");
//        result.put("resultCode", "0");
//        result.put("message", "success");
//        String sign = sign(result);
//        System.out.println("sing:"+sign);
//        String target2 = sm4Cipher.encrypt(sign, key);
//        System.out.println(target2);
//    }

    //签名与加密
    static String sign(Map<String, Object> param) {
        StringBuffer sb = new StringBuffer();
        Iterator<String> it = param.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            sb.append(key).append("=").append(param.get(key)).append("&");
        }
        sb.deleteCharAt(sb.length() - 1);
        Sm3Signature signature = new Sm3Signature();
        System.out.println(sb.toString());
        String sign = signature.signature(sb.toString());
        param.put("signature", sign);
        return JSONObject.toJSONString(param);
    }
}
